/*
 * Powered By xTOOLs
 * Dev by DuRisk@pactera.com
 * Copyright © 2009 - 2017 pactera.com. All rights reserved.
 *
 * Generated at 2017年06月29日 09:58:43
 *
 */

package com.pactera.jep.service.sys.web.controller;

import com.pactera.jep.autoconfigure.web.controller.BaseRestController;
import com.pactera.jep.core.exception.ServiceException;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.orm.Order;
import com.pactera.jep.orm.Page;
import com.pactera.jep.sys.model.StaffGroup;
import com.pactera.jep.sys.service.StaffGroupService;
import com.pactera.jep.web.code.WebCode;
import com.pactera.jep.web.entity.NameValuePair;
import com.pactera.jep.web.util.NVUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "用户组")
@RestController
@RequestMapping("/staffGroup")
public class StaffGroupRestController extends BaseRestController<StaffGroup> {
	private static Logger	logger	= LoggerFactory.getLogger(StaffGroupRestController.class);

	@Autowired
	private StaffGroupService	staffGroupService;
	/**
	 * 为列表页面做数据准备
	 */
	@Override
	protected void prepareQuery(String token, Page<StaffGroup> page, List<Filter> filters, List<Order> orders, HttpServletRequest request) {
		
		staffGroupService.query(page, filters, orders);
	}
	
	/**
	 * 保存
	 * @param staffGroup
	 * @return
	 */
	@ApiOperation(value = "保存一条数据")
	@PostMapping(value = "/save")
	public Map<String, Object> save(@RequestBody StaffGroup staffGroup) {
		
		Map<String, Object> tips = new HashMap<String, Object>();
		
		Boolean isExist = staffGroupService.exists(staffGroup.getStaffGroupCode());
		try 
		{
			if(isExist)
			{
				staffGroupService.update(staffGroup);
			}
			else
			{
				staffGroupService.insert(staffGroup);
			}
			
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
		}
		catch (ServiceException e) 
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
		}
		return tips;
	}
	
	/**
	 * 删除
	 * @param staffGroupCode
	 * @return
	 */
	@ApiOperation(value = "根据编码删除一条数据")
	@PostMapping(value = "/remove/{staffGroupCode}")
	public Map<String, Object> remove(@PathVariable String staffGroupCode){
		
		Map<String, Object> tips = new HashMap<String, Object>();
		
		try 
		{
			staffGroupService.deleteStaffGroup(staffGroupCode);
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_DELETE);
		} 
		catch (ServiceException e) 
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_DELETE);
		}
		
		return tips;
	}
	
	/**
	 * 验证提交的staffGroupCode是否已经存在数据库中
	 * @param staffGroupCode
	 * @return
	 */
	@ApiOperation(value = "验证提交的编码是否已经存在数据库中")
	@GetMapping(value = "/check", produces = "application/json;charset=UTF-8")
	public Map<String, Object> check(String staffGroupCode){
		
		Map<String, Object> tips = new HashMap<String, Object>();

		Boolean isExist = staffGroupService.exists(staffGroupCode);
		tips.put("valid", !isExist);

		return tips;
	}
	
	/**
	 * 以特殊格式获取所有用户组类型
	 * @return json格式返回
	 */
	@ApiOperation(value = "以特殊格式获取所有用户组类型")
	@GetMapping(value = "/queryAll")
	public List<NameValuePair> queryAll(){
		
		List<StaffGroup> result = staffGroupService.selectAll();
		
		return NVUtils.convert(result, result.size(), "staffGroupCode", "staffGroupName");
	}
}